<template>
  <div class="ms-list">

    <el-row class="operation">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">
          <icon style="" name="home"></icon>
        </el-breadcrumb-item>
        <el-breadcrumb-item>渠道推广系统</el-breadcrumb-item>
        <el-breadcrumb-item>推广管理</el-breadcrumb-item>
        <el-breadcrumb-item>运营数据报告</el-breadcrumb-item>
      </el-breadcrumb>
      <el-button-group>
        <el-tooltip class="item" effect="dark" placement="left-start">
          <div slot="content">
            导出数据为列表中的所有数据，<br/>
            如果有筛选条件，则导出数据<br/>为筛选后的所有数据。
          </div>
          <el-button type="primary" size="small" :data="search.list" @click="exportFile">
          <icon name="cloud-download"></icon> 导出数据
        </el-button>
        </el-tooltip>
      </el-button-group>
    </el-row>

    <ms-search :data="search.list" @click="updateConditions"></ms-search>

    <el-table
      v-loading="table.loading"
      class="element-table"
      size="small" stripe border
      :summary-method="getSummaries"
      show-summary
      :data="table.list"
      :highlight-current-row="true"
      :fit="true">
      <el-table-column prop="id" label="ID" min-width="80"></el-table-column>
      <el-table-column prop="day" label="日期" min-width="85">
        <template slot-scope="scope">
          {{scope.row.day.toString().substring(0, 4) + '-' + scope.row.day.toString().substring(4, 6) + '-' + scope.row.day.toString().substring(6, 8)}}
        </template>
      </el-table-column>
      <el-table-column prop="itemTitle" label="项目名称" min-width="140"></el-table-column>
      <el-table-column prop="platformId" label="平台" min-width="80">
        <template slot-scope="scope">
          <ms-platform :platform-id="scope.row.platformId"/>
        </template>
      </el-table-column>
      <el-table-column prop="clientId" label="客户端" min-width="80">
        <template slot-scope="scope">
          <ms-device :client-id="scope.row.clientId"/>
        </template>
      </el-table-column>
      <el-table-column prop="impression" label="展现量" min-width="90" align="right"
                       header-align="center"></el-table-column>
      <el-table-column prop="click" label="点击量" min-width="90" align="right" header-align="center"></el-table-column>
      <el-table-column prop="clickPrice" label="平均点击价格" min-width="90" align="right" header-align="center">
        <template slot-scope="scope">
          {{scope.row.click === 0 ? '∞' : (scope.row.costCoin / scope.row.click).toFixed(2)}}
        </template>
      </el-table-column>
      <el-table-column prop="clickRatio" label="点击率" min-width="90" align="right" header-align="center">
        <template slot-scope="scope">
          {{scope.row.impression === 0 ? '∞' : (scope.row.click / scope.row.impression * 100).toFixed(2) + '%'}}
        </template>
      </el-table-column>
      <el-table-column prop="advisoryRatio" label="有效咨询率" min-width="90" align="right" header-align="center">
        <template slot-scope="scope">
          {{scope.row.click === 0 ? '∞' : (scope.row.talk / scope.row.click * 100).toFixed(2) + '%'}}
        </template>
      </el-table-column>
      <el-table-column prop="feedback" label="留言数" min-width="90" align="right" header-align="center"></el-table-column>
      <el-table-column prop="talk" label="有效对话量" min-width="90" align="right" header-align="center"></el-table-column>
      <el-table-column prop="phone" label="要电话量" min-width="90" align="right" header-align="center"></el-table-column>
      <el-table-column prop="talkRatio" label="沟通有效率" min-width="90" align="right" header-align="center">
        <template slot-scope="scope">
          {{scope.row.talk === 0 ? '∞' : (scope.row.phone / scope.row.talk * 100).toFixed(2) + '%'}}
        </template>
      </el-table-column>
      <el-table-column prop="costCoin" label="消费" min-width="90" align="right" header-align="center">
        <template slot-scope="scope">
          {{scope.row.costCoin.toFixed(2)}}
        </template>
      </el-table-column>
      <el-table-column prop="server" label="客服" min-width="80"></el-table-column>
      <el-table-column prop="saler" label="销售" min-width="80"></el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page.sync="table.pagination.page"
      :total="table.pagination.totalRow"
      :page-size="table.pagination.pageSize"
      :page-sizes="table.pagination.pageSizes"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
  import MsDevice from '@/components/common/Device.vue'
  import MsPlatform from '@/components/common/Platform.vue'
  import MsSearch from '@/components/common/Search.vue'
  import MsConfirmButton from '@/components/common/ConfirmButton.vue'
  import Api from '@/api/customer/operating-data-report'
  import { SEARCH_CONDITION } from '@/models/customer/channel-popularize'
  import { PLATFORM_LIST } from '@/utils/constants'
  import ResponseParse from '@/utils/response-parse'

  export default {
    components: {
      MsDevice,
      MsPlatform,
      MsSearch,
      MsConfirmButton
    },
    data () {
      return {
        search: {
          list: [],
          condition: {}
        },
        table: {
          loading: true,
          list: [],
          pagination: {}
        }
      }
    },
    created () {
      this.search.list = SEARCH_CONDITION
      this.table.list = []
      this.requestList()
    },
    methods: {
      requestList () {
        this.table.loading = true
        const search = Object.assign({}, this.table.pagination, this.search.condition)
        Api.list({search: search}, response => {
          let status = response.status || 0
          let body = response.data || []
          if (status === 200 || status === 204) {
            this.table.list = body
            this.table.pagination = ResponseParse.pagination(response.headers)
//            if (this.table.pagination.totalPage === this.table.pagination.page) {
//              this.tbHeight = ''
//            } else {
//              this.$set(this, 'tbHeight', this.PHeigh - this.$refs.msSearch.clientHeight)
//            }
          } else {
//            this.tbHeight = ''
            this.table.list = []
            this.$message.error(body.error || '获取数据失败')
          }
          this.table.loading = false
        })
      },
      handleSizeChange (val) {
        this.table.pagination.pageSize = val
        this.requestList()
      },
      handleCurrentChange (val) {
        this.table.pagination.page = val
        this.requestList()
      },
      updateConditions (conditions) {
        this.table.pagination.page = 1
        this.search.condition = conditions
        this.requestList()
      },
      exportFile () {
        let date = this.search.list[0].value
        let platformTitle = this.search.list[1].value
        let itemTitle = this.search.list[2].value
        let platformId = 0
        let platforms = {}

        PLATFORM_LIST.forEach(item => {
          platforms[item.title] = item.id
        })

        if (platformTitle !== undefined && platformTitle !== '') {
          platformId = platforms[platformTitle]
        }
        let parameters1 = '?startTime=' + date[0] + '&endTime=' + date[1]
        let parameters2 = '&platformId=' + platformId
        let parameters3 = '&itemTitle=' + itemTitle

        window.location.href = '/rest/customer/report/adjust-reports/files' +
          parameters1 + (platformId > 0 ? parameters2 : '') + parameters3
      },
      getSummaries (param) {
        const {columns, data} = param
        const sums = []

        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '合计'
            return
          }
          const values = data.map(item => Number(item[column.property]))
          if (column.property === 'impression' || column.property === 'click' || column.property === 'feedback' ||
            column.property === 'talk' || column.property === 'phone' || column.property === 'costCoin') {
            if (!values.every(value => isNaN(value))) {
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr)
                if (!isNaN(value)) {
                  return prev + curr
                } else {
                  return prev
                }
              }, 0)
            }
          }
          if (column.property === 'costCoin') {
            sums[index] = sums[index].toFixed(2)
          }
        })

        sums[7] = sums[6] === 0 ? '∞' : (sums[14] / sums[6]).toFixed(2)
        sums[8] = sums[5] === 0 ? '∞' : (sums[6] / sums[5]).toFixed(2) + '%'
        sums[9] = sums[6] === 0 ? '∞' : (sums[11] / sums[6]).toFixed(2) + '%'
        sums[13] = sums[11] === 0 ? '∞' : (sums[12] / sums[11]).toFixed(2) + '%'

        return sums
      }
    }
  }
</script>

<style lang="scss" scoped>
  .element-table th {
    height: 48px;
  }

  .element-table td {
    height: 37px;
  }

  .item {
    margin: 4px;
  }
</style>
